import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:kasax/commons/commons.dart';

/// 使用示例
/// https://blog.csdn.net/qq_41614928/article/details/106878679
/// https://blog.csdn.net/chuyouyinghe/article/details/120090467
/// https://pub.flutter-io.cn/packages/flutter_easyrefresh#-readme-tab-
/// https://github.com/xuelongqy/flutter_easyrefresh/blob/v2/README.md
/// https://github.com/xuelongqy/flutter_easyrefresh/blob/v2/README.md
class EasyRefreshDemoHome extends StatefulWidget {
  const EasyRefreshDemoHome({Key? key}) : super(key: key);

  @override
  _EasyRefreshDemoHomeState createState() => _EasyRefreshDemoHomeState();
}

class _EasyRefreshDemoHomeState extends State<EasyRefreshDemoHome> {
  List<String> _list = [];
  EasyRefreshController _controller = EasyRefreshController();
  int numCount = 0;

  @override
  void initState() {
    super.initState();
    doInit();
  }

  @override
  void dispose() {
    super.dispose();
    numCount = 0;
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text(
            "EasyRefresh示例",
          ),
        ),
        body: EasyRefresh(
          firstRefresh: true,
          behavior: ScrollBehavior(),
          child: _buildCustomScrollView(),
          header: TaurusHeader(),
          footer: TaurusFooter(),
          onRefresh: () async {
            doInit();
          },
          onLoad: () async {
            doLoadMore();
          },
        ),
      ),
    );
  }

  CustomScrollView _buildCustomScrollView() {
    return CustomScrollView(
      slivers: [
        SliverFixedExtentList(
          delegate: SliverChildBuilderDelegate((context, index) {
            return ListTile(title: Text("item: ${_list[index]}"));
          }, childCount: _list.length),
          itemExtent: 60.0,
        )
      ],
    );
  }

  SafeArea buildSafeArea() {
    return SafeArea(
      child: buildContent(),
    );
  }

  buildContent() {
    return EasyRefresh(
      enableControlFinishLoad: true,
      enableControlFinishRefresh: true,
      controller: _controller,
      header: MaterialHeader(),
      // 头部刷新控件
      footer: MaterialFooter(),
      // 底部刷新控件
      onRefresh: () async {
        Fluttertoast.showToast(msg: "下拉刷新");
        setState(() {
          _list.clear();
        });
        doInit();
      },
      onLoad: () async {
        Fluttertoast.showToast(msg: "上拉加载更多");
        doLoadMore();
      },
      emptyWidget: _list.isEmpty ? buildLoadingWidget() : null,
      child: _list.isEmpty
          ? Container()
          : ListView.separated(
              itemBuilder: (context, index) {
                return ListTile(
                  title: Text("item: ${_list[index]}"),
                );
              },
              separatorBuilder: (context, index) {
                return Divider();
              },
              itemCount: _list.length),
    );
  }

  void doInit() {
    numCount = 0;
    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        for (int i = 0; i < 20; i++) {
          _list.add("index:$i");
        }
        numCount = _list.length;
        _controller.finishRefresh(success: true);
        // _controller.finishRefresh(success: true, noMore: false);
      });
    });
  }

  void doLoadMore() {
    Future.delayed(Duration(seconds: 2)).then((value) {
      setState(() {
        List<String> list = [];
        for (int i = 0; i < 20; i++) {
          list.add("index:(${numCount + i})");
        }
        numCount += list.length;
        _list.addAll(list);
        if (_list.length >= 100) {
          _controller.finishLoad(success: true, noMore: true);
        } else {
          _controller.finishLoad(
            success: true,
          );
        }
      });
    });
  }
}
